

using System;

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_MaxMagnitude_RT : RT_Node
    {
        public RT_SingleValuePort[] Values_RT;

        public RT_SingleValuePort Result_RT;

        public Node_MaxMagnitude_RT(Node_MaxMagnitude sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            Values_RT = BuildRTPortArrayFromGroup<RT_SingleValuePort>(EPortIOType.Input, sourceData.Values);

            Result_RT = new RT_SingleValuePort(EPortIOType.Output, sourceData.Result, this);
            Result_RT.OnGetValue = GetResult;
        }

        public AValue GetResult(DAG_Executor executor)
        {
            if (Values_RT.Length > 0)
            {
                var resultType = Result_RT.SourceValue.ValueType;
                if (resultType == AValue.EValueType.Integer)
                {
                    int result = Values_RT[0].GetValue(executor).ToInteger().Item2;
                    float maxAbs = MathF.Abs(result);
                    for (int i = 1; i < Values_RT.Length; i++)
                    {
                        int currInt = Values_RT[i].GetValue(executor).ToInteger().Item2;
                        float currAbs = MathF.Abs(currInt);
                        bool maxIsBigger = maxAbs > currAbs;
                        result = maxIsBigger ? result : currInt;
                        maxAbs = maxIsBigger ? maxAbs : currAbs;
                    }
                    Result_RT.SourceValue.SetInteger(result);
                }
                else if (resultType == AValue.EValueType.Scalar)
                {
                    float result = Values_RT[0].GetValue(executor).ToScalar().Item2;
                    float maxAbs = MathF.Abs(result);
                    for (int i = 1; i < Values_RT.Length; i++)
                    {
                        float currFloat = Values_RT[i].GetValue(executor).ToScalar().Item2;
                        float currAbs = MathF.Abs(currFloat);
                        bool maxIsBigger = maxAbs > currAbs;
                        result = maxIsBigger ? result : currFloat;
                        maxAbs = maxIsBigger ? maxAbs : currAbs;
                    }
                    Result_RT.SourceValue.SetScalar(result);
                }
            }
            return Result_RT.SourceValue;
        }
    }
}